package com.example.controller.sys;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.Menu;
import com.example.service.IMenuService;
import com.example.vo.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2022-03-27
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags = "菜单管理",value = "")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @ApiOperation(value = "获取权限配置的菜单选项")
    @GetMapping("/options")
    public ResultVo getMenuOptions(){
        return menuService.getMenuOptions();
    }

    @ApiOperation(value = "获取菜单管理页面数据")
    @GetMapping("/query")
    public ResultVo getMenus(String menuName,Integer enabled){
        return menuService.getMenus(menuName,enabled);
    }

    @ApiOperation(value = "添加菜单")
    @PostMapping("/add")
    public ResultVo addMenu(@RequestBody Menu menu){
        if(menuService.save(menu)){
            return ResultVo.success("添加成功。");
        }
        return ResultVo.error("添加失败!");
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public ResultVo updateMenu(@RequestBody Menu menu){
        return menuService.updateMenu(menu);
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/delete")
    public ResultVo deleteMenu(Integer[] ids){
        return menuService.deleteMenu(ids);
    }

    @ApiOperation(value = "导入菜单表数据")
    @PostMapping("/import")
    public void importMenus(MultipartFile file){
        menuService.importMenus(file);
    }

    @ApiOperation(value = "导出菜单表数据")
    @GetMapping("/export")
    public void exportMenus(HttpServletRequest request,HttpServletResponse response){
        menuService.exportMenus(request,response);
    }

    @ApiOperation(value = "查询是否存在相同路径的菜单")
    @GetMapping("/has-same-path")
    public boolean hasSameMenuPath(Integer id,String path){
        return menuService.hasSameMenuPath(id,path);
    }

    @ApiOperation(value = "查询同级目录下是否存在相同菜单名称")
    @GetMapping("/has-same-name-sibling")
    public boolean hasSameNameSiblingMenu(Integer id,@RequestParam(name = "menuName") String menuName,@RequestParam(name = "parentId") Integer parentId){
        return menuService.hasSameNameSiblingMenu(id,menuName,parentId);
    }
}

